package fv;

import java.util.ArrayList;
import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;

import com.ferdict.Outfit;

public class OutfitsDatabase {
	public static long id = 1;
	private static final CacheManager cacheManager = new CacheManager();
	private static final Cache cache = cacheManager.getCache("Outfits");

	public static Cache getCache() {
//		return cacheManager.getEhcache("Outfits");
		return cache;
	}

	public static void addOutfit(Outfit o) {
		o.setOutfitId(id);
		getCache().put(new Element("" + id, o));
		id++;
	}

	public static List<Outfit> getOutfits() {
		List<Outfit> outfits = new ArrayList<Outfit>();
		for (Object k : getCache().getKeys()) {
			Outfit o = (Outfit) (getCache().get((String) k)).getValue();
			if (o.isShare()) {
				outfits.add(o);
			}
		}

		return outfits;
	}

	public static List<Outfit> getOutfits(String username) {
		List<Outfit> outfits = new ArrayList<Outfit>(getCache().getKeys()
				.size());
		for (Object k : getCache().getKeys()) {
			Outfit o = (Outfit) (getCache().get((String) k)).getValue();
			if (o.getUsername().equals(username)) {
				outfits.add(o);
			}
		}

		return outfits;
	}

	public static List<String> getComments(long outfitId) {
		return ((Outfit) getCache().get("" + outfitId).getValue())
				.getCommentList();
	}

	public static void share(long outfitId) {
		Outfit o = (Outfit) getCache().get("" + outfitId).getValue();
		o.share();
		getCache().put(new Element("" + id, o));
	}

	public static void unshare(long outfitId) {
		Outfit o = (Outfit) getCache().get("" + outfitId).getValue();
		o.unshare();
		getCache().put(new Element("" + id, o));
	}

	public static void like(long outfitId) {
		Outfit o = (Outfit) getCache().get("" + outfitId).getValue();
		o.like();
		getCache().put(new Element("" + id, o));
	}

	public static void dislike(long outfitId) {
		Outfit o = (Outfit) getCache().get("" + outfitId).getValue();
		o.dislike();
		getCache().put(new Element("" + id, o));
	}

	public static void comment(long outfitId, String cmt) {
		Outfit o = (Outfit) getCache().get("" + outfitId).getValue();
		o.getCommentList().add(cmt);
		getCache().put(new Element("" + id, o));
	}

	public static void main(String[] a) {
		OutfitsDatabase ob = new OutfitsDatabase();
		for (int i = 1; i < 10; i++)
			OutfitsDatabase.getCache().put(new Element("a" + i, "hello"));

		System.out.println(OutfitsDatabase.getCache().getKeys().size());

	}
}
